<?php
namespace app\usermodule\controllers;

use app\models\OrderRecorder;
use app\models\RateRecorder;
use yii\db\Exception;

class RateController extends BaseController{

    public function actionList(){
        $id = \Yii::$app->request->post('id');
        $model = OrderRecorder::find()->select(['id','order_content'])->where('id=:id',[':id' => $id])
        ->andWhere('user_id='.$this->user->id)->one();
        if(empty($model)){
            $result = ['reuslt'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        $arr=[];
        $arr['id']=$model->id;
        $list = json_decode($model->order_content,true);
        $list = $list[0]['list'];
        $goods=[];
        foreach($list as $k=>$v){
            $goods[$k]['goods_name'] = $v['goods_name'];
            $goods[$k]['attr'] = $v['attr'];
            $goods[$k]['thumb_pic'] = $v['thumb_pic'];
        }
            $goods[$k]['goods_id'] = $v['id'];
        $arr['goods'] = $goods;
        $result = ['result'=>1,'data'=>$arr];
        return json_encode($result);
    }

    public function actionAddrate(){
        $post = \Yii::$app->request->post('form');
        $is_name = intval(\Yii::$app->request->post('is_name'));
        $order = intval(\Yii::$app->request->post('order'));
        $order_model = OrderRecorder::find()->where('id=:id',[':id' => $order])->andWhere('user_id='.$this->user->id)->one();
        if(!$order_model){
            $result = ['result'=>0,'data'=>'不存在相关订单，请重新操作'];
            return json_encode($result);
        }
        if($order_model->is_rate){
            $result = ['result'=>0,'data'=>'该订单已经评价'];
            return json_encode($result);
        }
        $commit = \Yii::$app->db->beginTransaction();
        try{
            foreach($post as $k=>$v){
                $model = new RateRecorder();
                $model->order_id = intval($v['order_id']);
                $model->goods_id = intval($v['goods_id']);
                $model->user_id = $this->user->id;
                $pic_arr=[];
                if(is_array($v['pics'])){
                    if(count($v['pics'])>\Yii::$app->params['rate_num']){
                        $result = ['result'=>0,'data'=>'上传图片不能超过'.\Yii::$app->params['rate_num'].'张'];
                    }
                    foreach($v['pics'] as $pv){
                        $pic_arr[]=str_replace(\Yii::$app->params['base_url'],"../",$pv);
                    }
                }
                $model->pics = json_encode($pic_arr);
                $model->content = $v['content'];
                $model->rate = intval($v['rate']);
                $model->addtime = time();
                $model->attr = is_array($v['attr'])?json_encode($v['attr']):json_encode([]);
                $model->is_name = $is_name;
                $model->save(false);
            }

                $order_model->is_rate=1;
                $order_model->save(false);
            $commit->commit();
            $result = ['result'=>1,'data'=>'操作成功'];
        }catch (Exception $e){
            $commit->rollBack();
            $result = ['result'=>0,'data'=>'网络发生错误，请重新操作'];
            echo $e;
        }
        return json_encode($result);
    }

    public function actionDelpic(){
        $pic = \Yii::$app->request->post('pic');
        if(!empty($pic)){
            $pic = str_replace(\Yii::$app->params['base_url'],'../',$pic);
            @unlink($pic);
        }
        $result = ['result'=>1];
        return json_encode($result);
    }

}
